package com.dongdongshop.service.impl.sms;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.dongdongshop.service.SmsService;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {
    private DefaultProfile profile = DefaultProfile.getProfile(
            "cn-qingdao",// 地域ID
            "LTAI4GG4mWWJ28D7QkD8F9LL",// 您的AccessKey ID
            "aEsWaIpvz3OYKDZefxtzwDIv5JIiRd");// 您的AccessKey Secret
    private IAcsClient client = new DefaultAcsClient(profile);

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }
    @Override
    public void sendSms(String phoneNumber,String code) throws ClientException {
        CommonRequest request = new CommonRequest();
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
        request.putQueryParameter("SignName", "srz商城");
        // 短信模板ID
        request.putQueryParameter("TemplateCode", "SMS_194061663");
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplatePar am", "{\"code\":"+code+"}");
        System.out.println(code);
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object bizId = map.get("BizId");
        System.out.println("BizId"+bizId.toString());
    }

}
